﻿///<reference path="../../scripts/jquery-1.4.4.min.js" />
///<reference path="../../scripts/kooboo/kooboo.form.js" />


$(function () {
    RenderOrders();
    RenderPager();

    $(".pagination").find("a").live("click", function (e) {
        e.preventDefault();
        $this = $(this);
        if ($this.attr("disabled") != "disabled" && $this.hasClass("current") == false) {
            var data = "Id=" + $("#CustomerId").val();
            var pageIndex = parseInt($(this).attr("index"));
            $this.ajaxGet({ success: function (data) {
                Orders = data;
                CurrentPageIndex = pageIndex;
                RenderOrders();
                RenderPager();
            }
            });
        }

        return false;
    });

    var tablink = $("a[href=" + window.location.hash + "]");
    if (tablink.size() > 0) {
        switchTab($(tablink.parent()));
    }

    function RenderOrders() {
        $("#order-container").empty();
        var isEven = true;
        for (var i = 0; i < Orders.length; i++) {
            isEven = !isEven;
            var order = Orders[i];
            var row = $("<tr>"
                          + "  <td></td>"
                          + "  <td class='numeral'></td>"
                          + "  <td class='numeral'></td>"
                          + "  <td></td>"
                          + "  <td></td>"
                          + "  <td class='action'><a class='o-icon view' href='order.html' title='View detail'>View</a></td>"
                          + "</tr>");

            row.find("td:eq(0)").html(order.Id); //Id
            row.find("td:eq(1)").html(order.Quantity); //Quantity
            row.find("td:eq(2)").html(order.Total); //Total
            row.find("td:eq(3)").html(order.OrderDate); //OrderDate
            row.find("td:eq(4)").html(order.OrderStatus); //OrderStatus
            //row.find("td:eq(5)").find(".view").attr("href", OrderDetailUrl + "/" + order.Id); //view url
            row.find("td:eq(5)").find(".view").attr("href", order.DetailUrl); //view url

            if (isEven) {
                row.addClass("even");
            }

            $("#order-container").append(row);

        }
    }

    function RenderPager() {
        var pagination = $(".pagination");
        if (PageCount > 1) {
            pagination.show();
            var pager = $(".pagination>div");
            pager.empty();

            pager.append($("<a>&lt;</a>"));

            for (var i = 1; i < PageCount + 1; i++) {
                if (i == CurrentPageIndex) {
                    pager.append("<a class='current' index='" + i + "'>" + i + "</a>");
                } else {
                    pager.append("<a index='" + i + "' href='" + OrdersUrl + "?Id=" + $("#CustomerId").val() + "&pageindex=" + i + "'>" + i + "</a>");
                }
            }

            pager.append($("<a>&gt;</a>"));

            if (CurrentPageIndex > 1) {
                pager.find("a:first").removeAttr("disabled").attr("href", OrdersUrl + "?Id=" + $("#CustomerId").val() + "&pageindex=" + (CurrentPageIndex - 1)).attr("index", (CurrentPageIndex - 1));
            } else {
                pager.find("a:first").removeAttr("href").attr("disabled", "disabled");
            }


            if (CurrentPageIndex < PageCount) {
                pager.find("a:last").removeAttr("disabled").attr("href", OrdersUrl + "?Id=" + $("#CustomerId").val() + "&pageindex=" + (CurrentPageIndex + 1)).attr("index", (CurrentPageIndex + 1));
            } else {
                pager.find("a:last").removeAttr("href").attr("disabled", "disabled");
            }
        } else {
            pagination.hide();
        }
    }
});

